package com.hisicom.scqxspxcjc.interfaces;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.hisicom.scqxspxcjc.base.bean.Result;
import com.hisicom.scqxspxcjc.base.enums.ResultEnum;
import com.hisicom.scqxspxcjc.sceneCheck.systemCheck.domain.SystemCheck;
import com.hisicom.scqxspxcjc.services.SystemCheckServiceBus;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @author zhonggls
 * @date 2023/9/8 15:36
 * @Explain
 */
@Controller
@RequestMapping("sceneCheckApi/systemCheck")
@Slf4j
public class SceneCheckApi {
    @Autowired
    SystemCheckServiceBus systemCheckServiceBus;

    @PostMapping(value = "add")
    @ResponseBody
    public Object addSystemCheck(@RequestBody String requset) {
        log.info("添加数据进来了："+requset);
        JSONObject jsonObject = JSON.parseObject(requset);
        if (!SignTool.verification(jsonObject)) {
            return Result.error(ResultEnum.ERROR_SIGNATURE_FAILURE);
        }
        JSONObject bodyJson = jsonObject.getJSONObject("body");
        JSONArray jsonArray = bodyJson.getJSONArray("list");
        List<SystemCheck> systemCheckList = JSONArray.parseArray(jsonArray.toJSONString(), SystemCheck.class);
        if (systemCheckList != null && systemCheckList.size() > 0) {
            for (SystemCheck systemCheck : systemCheckList) {
                if (systemCheck.getFlowName().equals("合并现场检查")) {
                    systemCheck.setFlowName("现场检查");
                }
               if (systemCheck.getFlowName().equals("合并现场复查")) {
                    systemCheck.setFlowName("现场复查");
                }
               systemCheck.setCheckStatus("0");
                systemCheckServiceBus.addSyctemCheck(systemCheck);
            }
        }
        return Result.success();
    }

    @PostMapping(value = "get")
    @ResponseBody
    public Object getSystemCheck(@RequestBody String requset) {
        JSONObject jsonObject = JSON.parseObject(requset);
        if (!SignTool.verification(jsonObject)) {
            return Result.error(ResultEnum.ERROR_SIGNATURE_FAILURE);
        }
        return Result.success(systemCheckServiceBus.findByCheckStatus());
    }

    @PostMapping(value = "finish")
    @ResponseBody
    public Object finish(@RequestBody String requset) {
        JSONObject jsonObject = JSON.parseObject(requset);
        if (!SignTool.verification(jsonObject)) {
            return Result.error(ResultEnum.ERROR_SIGNATURE_FAILURE);
        }
        JSONObject bodyJson = jsonObject.getJSONObject("body");
        String groupId = bodyJson.getString("groupId");
        if (StrUtil.isNotBlank(groupId)) {
            systemCheckServiceBus.finish(groupId.split(","));
        }
        return Result.success();
    }
}
